package 算法.练习1;

/**
 * @author:谢君臣
 * @Date:2021/9/10-17:06
 * @version:1.0
 * @function:
 */
public class 单链表相交 {
    public static void main(String[] args) {
        Node temp =new Node(3,new Node(5));
        Node root1 = new Node(1,new Node(2,temp));
        Node root2 = new Node(1,temp);
        get(root1,root2);
    }
    public static void get (Node headA,Node headB){
        Node temp1 = headA;
        Node temp2 = headB;
        if (headA==null||headB==null)
            return;
        while (temp1!=temp2){
            if(temp1 ==null)
                temp1 = headB;
            if (temp2 ==null)
                temp2 = headA;
            temp1 = temp1.next;
            temp2 = temp2.next;
        }
        System.out.println(temp1.val);

    }
}
class Node{
    int val;
    Node next;

    public Node(int val) {
        this.val = val;
    }

    public Node(int val, Node next) {
        this.val = val;
        this.next = next;
    }
}